package com.bw.employee.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.employee.mapper.TbSectionMapper;
import com.bw.po.TbPermissions;
import com.bw.employee.service.TbPermissionsService;
import com.bw.employee.mapper.TbPermissionsMapper;
import com.bw.po.TbSection;
import com.bw.result.ResponseState;
import com.bw.result.ResultVo;
import com.bw.vo.CascadeVo;
import com.bw.vo.IdVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 花开Mystic
* @description 针对表【tb_permissions(权限表)】的数据库操作Service实现
* @createDate 2023-11-11 14:58:27
*/
@Service
public class TbPermissionsServiceImpl extends ServiceImpl<TbPermissionsMapper, TbPermissions>
    implements TbPermissionsService{

    @Autowired
    private TbPermissionsMapper tbPermissionsMapper;

    @Autowired
    private TbSectionMapper tbSectionMapper;

    @Override
    public ResultVo getCascadeInfo(IdVo idVo) {
        //--1 先获取此部门拥有的权限
        if(idVo.getId().isEmpty()){
            return ResultVo.FAILED(ResponseState.DATA_DO_NOT_NULL);
        }
        QueryWrapper<TbSection> sectionWrapper = new QueryWrapper<>();
        sectionWrapper.lambda().eq(TbSection::getId,idVo.getId());
        TbSection tbSection = tbSectionMapper.selectOne(sectionWrapper);
        if(tbSection == null){
            return ResultVo.FAILED(ResponseState.NOT_EXIST);
        }

        //--2 对此部门的权限进行切割处理 并放入集合
        ArrayList<Integer> ownList = new ArrayList<>();
        String sectionPermissions = tbSection.getPermissions();
        if(sectionPermissions != null && sectionPermissions != ""){
            String[] split = tbSection.getPermissions().split(":");
            for (String s : split) {
                ownList.add(Integer.valueOf(s));
            }
        }

        //--3 先获取第一级的列表
        QueryWrapper<TbPermissions> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TbPermissions::getPid,0);
        List<TbPermissions> firstGradeList = list(wrapper);

        //--4 获取所有信息列表
        List<TbPermissions> list = list();

        //--5 获取一级权限CascadeVo列表 并判断是否选中
        List<CascadeVo> cascadeList = firstGradeList.stream().map(item -> {
            CascadeVo cascadeVo = new CascadeVo();
            cascadeVo.setId(item.getId());
            cascadeVo.setName(item.getName());
            if(ownList.size()>0 && ownList.contains(item.getId())) {
                cascadeVo.setOwn(true);
            } else {
                cascadeVo.setOwn(false);
            }
            return cascadeVo;
        }).collect(Collectors.toList());

        //--6 递归填补子权限集合信息
        cascadeList.forEach(item->{
            recursionFinishCascade(item,list,ownList);
        });

        return ResultVo.SUCCESS(cascadeList);
    }

    private void recursionFinishCascade(CascadeVo cascadeVo,List<TbPermissions> list,ArrayList<Integer> ownList){
        Integer pid = cascadeVo.getId();
        List<CascadeVo> collect = list.stream().filter(item -> {
            return item.getPid() == pid;
        }).map(item -> {
            CascadeVo cascade = new CascadeVo();
            cascade.setId(item.getId());
            cascade.setName(item.getName());
            if(ownList.size()>0 && ownList.contains(item.getId())) {
                cascade.setOwn(true);
            } else {
                cascade.setOwn(false);
            }

            recursionFinishCascade(cascade,list,ownList);
            return cascade;
        }).collect(Collectors.toList());
        if(collect.size()>0){
            cascadeVo.setChildren(collect);
        }
    }
}




